﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "GmStructureDefaultElementParametersCustomizer.h"
#include "DbObjectUtils.h"
#include "IDocument.h"
#include "IElement.h"
#include "IParameter.h"
#include "OwnerPtr.h"
#include "IInstance.h"
#include "IElementParameters.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

void GmStructureDefaultElementParametersCustomizer::SetElementParametersCustomizer(IElement* pOwnerElement,
    OwnerPtr<IElementParametersCustomizer> opNewCustomizer)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pOwnerElement && opNewCustomizer, L"invalid input arguments",L"GDMPLab",L"2024-03-30");

    IElementParameters* pElementParameters = pOwnerElement->GetElementParameters();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pElementParameters, L"element parameters is null.",L"GDMPLab",L"2024-03-30");

    OwnerPtr<IElementParametersCustomizer> opOldCustomizer = pElementParameters->SetElementParametersCustomizer(TransferOwnership(opNewCustomizer));
    if (opOldCustomizer)
    {
        GmStructureDefaultElementParametersCustomizer* pCustomizer = quick_cast<GmStructureDefaultElementParametersCustomizer>(
            pOwnerElement->GetElementParameters()->GetParametersCustomizer());
        DBG_WARN_AND_RETURN_VOID_UNLESS(pCustomizer, L"GmStructureDefaultElementParametersCustomizer is null.",L"GDMPLab",L"2024-03-30");
        pCustomizer->SetProxyCustomizer(TransferOwnership(opOldCustomizer));
    }
}

DBOBJECT_DATA_DEFINE(GmStructureDefaultElementParametersCustomizer)
{
    m_pOwnerElement = nullptr;
}

GmStructureDefaultElementParametersCustomizer::GmStructureDefaultElementParametersCustomizer(IElement* pElement) : m_pOwnerElement(pElement), m_ProxyCustomizer(nullptr)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(m_pOwnerElement, L"owner element is null.",L"GDMPLab",L"2024-03-30");
}

OwnerPtr<IParameter> GmStructureDefaultElementParametersCustomizer::GetNativeParameter(int paramDefId) const
{
    auto pDefaultInstanceCustomizer = IElementParametersCustomizer::CreateDefaultInstanceParametersCustomizer(m_pOwnerElement);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDefaultInstanceCustomizer, L"proxy customizer is null.",L"GDMPLab",L"2024-03-30");
    return pDefaultInstanceCustomizer->GetNativeParameter(paramDefId);
}

bool GmStructureDefaultElementParametersCustomizer::SetNativeParameter(const IParameter* param, std::wstring* errorMsg)
{
    auto pDefaultInstanceCustomizer = IElementParametersCustomizer::CreateDefaultInstanceParametersCustomizer(m_pOwnerElement);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDefaultInstanceCustomizer, L"proxy customizer is null.",L"GDMPLab",L"2024-03-30");
    return pDefaultInstanceCustomizer->SetNativeParameter(param, errorMsg);
}

void GmStructureDefaultElementParametersCustomizer::ReportParameterDefinitions(std::vector<int>* pParamDefIds) const
{
    auto pDefaultInstanceCustomizer = IElementParametersCustomizer::CreateDefaultInstanceParametersCustomizer(m_pOwnerElement);
    DBG_WARN_AND_RETURN_VOID_UNLESS(pDefaultInstanceCustomizer, L"proxy customizer is null.",L"GDMPLab",L"2024-03-30");
    pDefaultInstanceCustomizer->ReportParameterDefinitions(pParamDefIds);
}

bool GmStructureDefaultElementParametersCustomizer::IsParameterModifiable(int parameterId) const
{
    auto pDefaultInstanceCustomizer = IElementParametersCustomizer::CreateDefaultInstanceParametersCustomizer(m_pOwnerElement);
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pDefaultInstanceCustomizer, L"proxy customizer is null.",L"GDMPLab",L"2024-03-30");
    return pDefaultInstanceCustomizer->IsParameterModifiable(parameterId);
}

NdbObject* gcmp::GmStructureDefaultElementParametersCustomizer::GetTopOwnerObject() const
{
    return nullptr;
}

void gcmp::GmStructureDefaultElementParametersCustomizer::SetProxyCustomizer(OwnerPtr<IElementParametersCustomizer> proxyCustomizer)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(proxyCustomizer, L"invalid input argument.",L"GDMPLab",L"2024-03-30");
    m_ProxyCustomizer = TransferOwnership(proxyCustomizer);
    m_ProxyCustomizer->SetOwnerElement(m_pOwnerElement);
}
